home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1986-06-19 | 5.9 KB | 184 lines |
- /* These are the basic predicates for list
- manipulation as defined by section 7.5,
- "Programming in Prolog" by Clocksin and
- Mellish. Since the predicates defined in
- the above referenced book do not function
- under TURBO PROLOG, some minor and major
- modifications had to be made. If you
- should happend to find an easier way to
- perform these predicates, please drop
- a copy of your source on the bulletin
- board. Thanx.
-
- A. J. La Rocque
-
- */
-
-
- domains
-
- number = integer
- character = char
- word = string
- attribute = symbol
-
- list_of_numbers = number*
- list_of_characters = character*
- list_of_words = word*
- list_of_attributes = symbol*
-
-
- predicates
-
- addlist(attribute,list_of_attributes,list_of_attributes).
- addlist(character,list_of_characters,list_of_characters).
- addlist(number,list_of_numbers,list_of_numbers).
- addlist(word,list_of_words,list_of_words).
-
- append(list_of_attributes,list_of_attributes,list_of_attributes).
- append(list_of_characters,list_of_characters,list_of_characters).
- append(list_of_numbers,list_of_numbers,list_of_numbers).
- append(list_of_words,list_of_words,list_of_words).
-
- delete(attribute,list_of_attributes,list_of_attributes).
- delete(character,list_of_characters,list_of_characters).
- delete(number,list_of_numbers,list_of_numbers).
- delete(word,list_of_words,list_of_words).
-
- efface(attribute,list_of_attributes,list_of_attributes).
- efface(character,list_of_characters,list_of_characters).
- efface(number,list_of_numbers,list_of_numbers).
- efface(word,list_of_words,list_of_words).
-
- head(list_of_attributes,attribute).
- head(list_of_characters,character).
- head(list_of_numbers,number).
- head(list_of_words,word).
-
- intersection(list_of_attributes,list_of_attributes,list_of_attributes).
- intersection(list_of_characters,list_of_characters,list_of_characters).
- intersection(list_of_numbers,list_of_numbers,list_of_numbers).
- intersection(list_of_words,list_of_words,list_of_words).
-
- last(attribute,list_of_attributes).
- last(character,list_of_characters).
- last(number,list_of_numbers).
- last(word,list_of_words).
-
- member(attribute,list_of_attributes).
- member(character,list_of_characters).
- member(number,list_of_numbers).
- member(word,list_of_words).
-
- nextto(attribute,attribute,list_of_attributes).
- nextto(character,character,list_of_characters).
- nextto(number,number,list_of_numbers).
- nextto(word,word,list_of_words).
-
- prefix(list_of_attributes,list_of_attributes).
- prefix(list_of_characters,list_of_characters).
- prefix(list_of_numbers,list_of_numbers).
- prefix(list_of_words,list_of_words).
-
- reverse(list_of_attributes,list_of_attributes).
- reverse(list_of_characters,list_of_characters).
- reverse(list_of_numbers,list_of_numbers).
- reverse(list_of_words,list_of_words).
-
- sublist(list_of_attributes,list_of_attributes).
- sublist(list_of_characters,list_of_characters).
- sublist(list_of_numbers,list_of_numbers).
- sublist(list_of_words,list_of_words).
-
- subset(list_of_attributes,list_of_attributes).
- subset(list_of_characters,list_of_characters).
- subset(list_of_numbers,list_of_numbers).
- subset(list_of_words,list_of_words).
-
- subst(attribute,list_of_attributes,attribute,list_of_attributes).
- subst(character,list_of_characters,character,list_of_characters).
- subst(number,list_of_numbers,number,list_of_numbers).
- subst(word,list_of_words,word,list_of_words).
-
- tail(list_of_attributes,list_of_attributes).
- tail(list_of_characters,list_of_characters).
- tail(list_of_numbers,list_of_numbers).
- tail(list_of_words,list_of_words).
-
- union(list_of_attributes,list_of_attributes,list_of_attributes).
- union(list_of_characters,list_of_characters,list_of_characters).
- union(list_of_numbers,list_of_numbers,list_of_numbers).
- union(list_of_words,list_of_words,list_of_words).
-
- clauses
-
- /*
- Finding the last element of a list: the goal
- last(X,L) succeeds if element X is the last
- element of list L. The boundary condition is
- when there is only one element in L.
- */
-
- last(X,[Y]) :- X = Y,!.
- last(X,[_|Y]) :- last(X,Y).
-
-
- append([],L,L).
- append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
-
- reverse([],[]).
- reverse([Head|Tail],List) :- reverse(Tail,Z),
- append(Z,[Head],List).
-
- efface(A,[X|L],L) :- A = X,!.
- efface(A,[B|L],[B|M]) :- efface(A,L,M).
-
- delete(_,[],[]).
- delete(X,[Y|L],M) :- X = Y,!, delete(X,L,M).
- delete(X,[Y|L1],[Y|L2]) :- delete(X,L1,L2).
-
- subst(_,[],_,[]).
- subst(X,[Y|L],A,[B|M]) :- X = Y and A = B,!,subst(X,L,A,M).
- subst(X,[Y|L],A,[Y|M]) :- subst(X,L,A,M).
-
- sublist([X|L],[X|M]) :- prefix(L,M),!.
- sublist(L,[_|M]) :- sublist(L,M).
-
- prefix([],_).
- prefix([X|L],[X|M]) :- prefix(L,M).
-
- member(X,[Y|_]) :- X = Y.
- member(X,[_|Y]) :- member(X,Y).
-
- subset(X,Y) :- head(X,A), member(A,Y),!, efface(A,X,Z), subset(Z,Y).
- subset([],_).
-
- tail([_|X],Y) :- X = Y.
-
- head([X|_],Y) :- Y = X.
-
- nextto(X,Y,L) :- head(L,A),
- A = X,
- tail(L,B),
- head(B,C),
- C = Y.
- nextto(X,Y,[_|L]) :- nextto(X,Y,L).
-
-
- intersection([],_,[]).
-
- intersection([X|A],B,[Y|C]) :-
- head([X|A],H),
- member(H,B),
- !,
- X = Y,
- intersection(A,B,C).
- intersection([_|R],Y,Z) :- intersection(R,Y,Z).
-
- addlist(A,L1,[B|L1]) :- A = B.
-
- union([],X,X).
- union([X|R],Y,Z) :- head([X|R],H), member(H,Y), !, union(R,Y,Z).
- union([X|R],Y,[X|Z]) :- union(R,Y,Z).
-
-